package com.yutel.phone.binh.data.xmlhandler;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.yutel.phone.binh.data.vo.Episode;
import com.yutel.phone.binh.data.vo.EpisodeSource;
import com.yutel.phone.binh.data.vo.EpisodeVideo;

public class SAXforEpisodeHandler extends DefaultHandler {
	private EpisodeSource source;
	private EpisodeVideo video;
	private Episode episode;

	public EpisodeSource getSource() {
		return source;
	}
	/**
	 * 开始读取内容
	 */
	public void startElement(String uri, String localname, String qname,
			Attributes attributes) throws SAXException {
		if("result".equals(localname)) {
			source = new EpisodeSource();
			source.setType(attributes.getValue("sourceType"));
			source.setVersion(attributes.getValue("version"));
			source.setList(new ArrayList<EpisodeVideo>());
		} else if ("video".equals(localname)) {
			video = new EpisodeVideo();
			video.setName(attributes.getValue("name"));
			video.setList(new ArrayList<Episode>());
		} else if ("episode".equals(localname)) {
			episode = new Episode();
			episode.setName(attributes.getValue("name"));
			episode.setIndexNum(attributes.getValue("indexNum"));
			episode.setUrl(attributes.getValue("url"));
		}
	}

	public void endElement(String uri, String localName, String qname)
			throws SAXException {
		if ("video".equals(localName) && video != null) {
			source.getList().add(video);
			video = null;
		}
		if ("episode".equals(localName) && episode != null) {
			video.getList().add(episode);
			episode = null;
		}
	}
}
